package com.lty.jeeg.server.modules.fas.controller;

import com.lty.jeeg.server.common.dto.ResponseInfo;
import com.lty.jeeg.server.common.enums.ResponseEnum;
import com.lty.jeeg.server.common.utils.BeanConverter;
import com.lty.jeeg.server.common.utils.ResponseUtil;
import com.lty.jeeg.server.common.utils.UserUtil;
import com.lty.jeeg.server.modules.fas.model.*;
import com.lty.jeeg.server.modules.fas.service.*;
import com.lty.jeeg.server.modules.sys.dao.UserDao;
import com.lty.jeeg.server.modules.sys.model.Dept;
import com.lty.jeeg.server.modules.sys.model.User;
import com.lty.jeeg.server.modules.sys.service.DeptService;
import com.lty.jeeg.server.modules.sys.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lty.jeeg.server.common.page.table.PageTableRequest;
import com.lty.jeeg.server.common.page.table.PageTableResponse;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author : zl
 * @date : 2019-02-17 09:18:29
 * Description : 采购明细
 */
@Api(tags = "采购明细")
@RestController
@RequestMapping("/orderDetail")
public class OrderDetailController {

    private final OrderDetailService orderDetailService;

    @Autowired
    private OrderService orderService;

    @Autowired
    public OrderDetailController(OrderDetailService orderDetailService) {
        this.orderDetailService = orderDetailService;
    }

    @GetMapping("/getByParentId/{orderId}")
    @ApiOperation(value = "根据orderId获取")
    public ResponseInfo getList(@PathVariable Long orderId) {
        return ResponseUtil.success(orderDetailService.getByorderId(orderId));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public ResponseInfo get(@PathVariable Long id) {
        return ResponseUtil.success(orderDetailService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "保存")
    public ResponseInfo save(@RequestBody OrderDetail orderDetail) {
        return ResponseUtil.success(orderDetailService.save(orderDetail));
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public ResponseInfo update(@RequestBody OrderDetail orderDetail) {
        return ResponseUtil.success(orderDetailService.update(orderDetail));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public ResponseInfo delete(@PathVariable Long id) {
        return ResponseUtil.success(orderDetailService.delete(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return orderDetailService.list(request);
    }

    @PostMapping("/deliver")
    @ApiOperation("交付")
    public ResponseInfo deliver(HttpServletRequest request, HttpServletResponse response) {
        ResponseEnum em = orderDetailService.deliver(request);
        if (em.getCode() == 0) {
            return ResponseUtil.success(em);
        } else {
            return ResponseUtil.error(em);
        }
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消交付")
    public ResponseInfo cancel(@PathVariable Long id) {
        ResponseEnum em = orderDetailService.cancel(id);
        if (em.getCode() == 0) {
            return ResponseUtil.success(em);
        } else {
            return ResponseUtil.error(em);
        }
    }


}
